package com.itbaizhan.openhis.service;
import cn.hutool.core.map.MapUtil;
import com.itbaizhan.openhis.api.domain.User;
import com.itbaizhan.openhis.constant.Constants;
import com.itbaizhan.openhis.constant.SecurityConstants;
import com.itbaizhan.openhis.constant.UserConstants;
import com.itbaizhan.openhis.domain.R;
import com.itbaizhan.openhis.exception.ServiceException;
import com.itbaizhan.openhis.fegin.RemoteUserService;
import com.itbaizhan.openhis.model.LoginUser;
import com.itbaizhan.openhis.redis.service.RedisService;
import com.itbaizhan.openhis.utils.SecurityUtils;
import com.itbaizhan.openhis.utils.StringUtils;
import com.itbaizhan.openhis.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 登录校验方法
 *
 * @author ruoyi
 */
@Component
public class SysLoginService
{
    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private SysPasswordService passwordService;

    /**
     * 登录
     */
    public User login(String username, String password)
    {
        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(username, password))
        {
            throw new ServiceException("用户/密码必须填写");
        }
        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            throw new ServiceException("用户密码不在指定范围");
        }
        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH)
        {
            throw new ServiceException("用户名不在指定范围");
        }

        // 查询用户信息
        R<User> userResult = remoteUserService.getUserInfo(username);

        if (StringUtils.isNull(userResult) || null == userResult.getData())
        {
            throw new ServiceException("登录用户：" + username + " 不存在");
        }
        User user = userResult.getData();
        passwordService.validate(user, password);
        return user;
    }

    public void logout(String loginName)
    {
//        recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "退出成功");
    }

    /**
     * 注册
     */
//    public void register(String username, String password)
//    {
//        // 用户名或密码为空 错误
//        if (StringUtils.isAnyBlank(username, password))
//        {
//            throw new ServiceException("用户/密码必须填写");
//        }
//        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
//                || username.length() > UserConstants.USERNAME_MAX_LENGTH)
//        {
//            throw new ServiceException("账户长度必须在2到20个字符之间");
//        }
//        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
//                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
//        {
//            throw new ServiceException("密码长度必须在5到20个字符之间");
//        }
//
//        // 注册用户信息
//        User sysUser = new User();
//        sysUser.setUserName(username);
//        sysUser.setPassword(SecurityUtils.encryptPassword(password));
//        AjaxResult registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
//
//        if (AjaxResult.FAIL == registerResult.getCode())
//        {
//            throw new ServiceException(registerResult.getMsg());
//        }
//        recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功");
//    }
}
